<?php
/**
 * @Name: NodeServices.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace app\common\services\rbac;

use app\common\logic\rbac\NodeLogic;
use app\common\model\rbac\UsersRoleModel;
use app\common\services\BaseServices;
use HugCode\PhpUnits\Frame\Code\ConstCode;
use HugCode\PhpUnits\Utils\UtilArray;

class NodeServices extends BaseServices
{


    public function __construct(NodeLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * @Desc 菜单列表
     * @param array $params
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function getNotPageList(array $params = [])
    {
        $where = [
            'NodeStatus' => ConstCode::BASIC_STATUS_NORMAL,
        ];
        if (isset($params['ShowStatus'])) {
            $where['NodeShowType'] = intval($params['ShowStatus']);
        }
        $select = [
            'NodeID', 'ControllerName', 'ActionName', 'NodeName', 'NodeOrder', 'NodeShowType',
            'NodeStatus', 'NodeType', 'ParentID', 'NodeIcon'
        ];
        $order  = ['ParentID' => 'asc', 'NodeOrder' => 'asc'];
        $list   = $this->selectList($where, $select, 0, 0, [], $order)->toArray();

        $map = [];
        foreach ($list as $item) {
            $nodeID       = $item['NodeID'];
            $map[$nodeID] = $nodeID;
        }
        $newList = [];
        foreach ($list as $item) {
            if ($item['ParentID'] == 0 || isset($map[$item['ParentID']])) {
                $newList[] = $item;
            }
        }
        return [
            'list'  => $newList,
            'count' => count($newList)
        ];
    }

    /**
     * @Desc 获取用户所有菜单节点树
     * @param int $userId
     * @param bool $selectButton
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function getUserNode(int $userId, bool $selectButton = false)
    {
        /** @var UsersServices $userService */
        $userService = app()->make(UsersServices::class);
        $info        = $userService->get($userId, ['IsSuper', 'UserID']);
        $select      = [
            'NodeID as menuId',
            'NodeName as title',
            'ParentID as parentId',
            'ControllerName',
            'ActionName',
            'NodeIcon as icon',
            'NodeShowType'
        ];
        $where       = [
            ['NodeStatus', '=', ConstCode::BASIC_STATUS_NORMAL],
        ];
        if ($selectButton === false) {
            $where[] = ['NodeType', '=', ConstCode::NODE_TYPE_MENU];
        }
        if ($info['IsSuper'] !== ConstCode::IS_YES) {
            $where[] = ['NodeID', 'in', $this->getUserNodeIDArray($userId)];
        }
        $result = $this->selectList($where, $select, 0, 0, [], ['NodeOrder' => 'asc'])->toArray();
        return array_column($result, null, 'menuId');
    }

    /**
     * @Desc 获取用户所有菜单节点ID数组
     * @param int $userId
     * @return mixed
     * @author yashuai<1762910894@qq.com>
     */
    public function getUserNodeIDArray(int $userId)
    {
        /** @var RoleNodeServices $roleNodeService */
        $roleNodeService = app()->make(RoleNodeServices::class);

        $where = [['RoleID', 'in', function ($query) use ($userId) {
            $table = app()->make(UsersRoleModel::class)->getTable();
            $query->table($table)->where(['UserID' => $userId])->field('RoleID');
        }]];

        return $roleNodeService->column($where, 'NodeID');
    }

    /**
     * @Desc 获取用户菜单节点
     * @param int $userId
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function getUserAdminNodeTree(int $userId)
    {
        $node = $this->getUserNode($userId);
        foreach ($node as &$val) {
            $val['hide'] = $val['NodeShowType'] == ConstCode::IS_NO;
            if ($val['parentId'] != 0) {
                $val['path'] = $val['component'] = "/" . $val['ControllerName'] . "/" . $val['ActionName'];
                unset($val['ControllerName'], $val['ActionName']);
            }
            unset($val['NodeShowType']);
        }
        $node = UtilArray::listToTree($node, 'menuId', 'parentId', 'children');

        foreach ($node as &$val) {
            if ($val['parentId'] == 0) {
                if (isset($val['children'])) {
                    $val['component'] = '';
                    $val['path']      = "/" . $val['ControllerName'];
                } else {
                    $val['path'] = $val['component'] = "/" . $val['ControllerName'] . "/" . $val['ActionName'];
                }
                unset($val['ControllerName'], $val['ActionName']);
            }
        }

        return $node;
    }


}
